package com.sjm.bookshop.controller.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjm.bookshop.entity.BType;
import com.sjm.bookshop.entity.Book;
import com.sjm.bookshop.entity.BookS;
import com.sjm.bookshop.entity.Slide;
import com.sjm.bookshop.service.book.BookService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    //查询分类列表
    @RequestMapping("/typeList")
    public Result bookList(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<BType> books = bookService.getBooks();
        PageInfo<BType> pageInfo = new PageInfo<>(books);
        return Result.success(200, pageInfo, "success");
    }

    //    修改状态
    @RequestMapping("/changeStatus/{tid}/{typestatus}")
    public Result changeStatus(@PathVariable("tid") int tid, @PathVariable("typestatus") int typestatus) {
        if ((typestatus == 0 || typestatus == 1) && tid > 0) {
            int result = bookService.changeSta(tid, typestatus);
            if (result > 0) {
                return Result.success(200, "修改成功");
            } else {
                return Result.fail(201, "修改失败");
            }
        } else {
            return Result.fail(202, "错误请求");
        }

    }

    //添加分类
    @RequestMapping("/addType")
    public Result addType(@RequestBody BType bType) {
        System.out.println(bType);
        if (bType != null) {
            int result = bookService.add(bType);
            if (result > 0) {
                return Result.success(200, "添加成功");
            } else {
                return Result.fail(201, "重复添加类别");
            }
        } else {
            return Result.fail(202, "错误请求");
        }

    }


    //    删除分类
    @RequestMapping("/delType/{tid}")
    public Result delType(@PathVariable("tid") int tid) {
        if (tid >= 0) {
            int result = bookService.del(tid);
            if (result > 0) {
                return Result.success(200, "删除成功");
            } else {
                return Result.fail(201, "删除失败");
            }
        } else {
            return Result.fail(202, "错误请求");
        }

    }

    //    类别查询
    @RequestMapping("/selectByKey")
    public Result selectType(int page, int limit, String keyWord) {
        if (page > 0 && limit > 0 && (keyWord != null || keyWord.equals(null))) {
            PageHelper.startPage(page, limit);
            System.out.println("keyWord------------------>"+keyWord);
            List<BType> bTypes = bookService.select(keyWord);
            PageInfo<BType> pageInfo = new PageInfo<>(bTypes);
            if (pageInfo.getList().size() > 0) {
                return Result.success(200, pageInfo.getList(), "查询到以下结果");
            } else {
                return Result.fail(202, "未查到结果");
            }
        } else {
            return Result.fail(201, "错误请求");
        }

    }


    // 查询所有图书
    @RequestMapping("/showAllBook")
    public Result selectBook(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<BookS> bookList = bookService.selectAll();
        PageInfo<BookS> pageInfo = new PageInfo<>(bookList);
        return Result.success(200, pageInfo, "success");
    }


    // 添加图书
    @RequestMapping("/addBook")
    public Result addBook(@RequestBody Book book) {
        if (book != null) {
            int i = bookService.addBook(book);
            if (i > 0) {
                return Result.success(200, "添加成功");
            } else {
                return Result.fail(201, "添加失败");
            }
        } else {
            return Result.fail(202, "添加失败,不可添加空记录");
        }

    }

    //    删除
    @RequestMapping("/delBook/{bookId}")
    public Result delBook(@PathVariable int bookId) {
        if (bookId != 0) {
            int res = bookService.deleteBook(bookId);
            if (res > 0) {
                return Result.success(200, "成功删除一条记录");
            } else {
                return Result.fail(201, "删除失败");
            }
        } else {
            return Result.fail(202, "错误请求");
        }
    }

    //      修改上架
    @RequestMapping("/isOn/{bookId}/{ison}")
    public Result changeBookIsOn(@PathVariable("bookId") int bookId, @PathVariable("ison") int ison) {
        if (bookId > 0 && (ison == 0 || ison == 1)) {
            int res = bookService.changeBook(bookId, ison);
            if (res > 0) {
                return Result.success(200, "成功修改状态");
            } else return Result.fail(201, "修改状态失败");
        } else return Result.fail(202, "错误请求");
    }

    // 推荐
    @RequestMapping("/recommend/{bookId}/{isrecommend}")
    public Result changeBookRecommend(@PathVariable("bookId") int bookId, @PathVariable("isrecommend") int isrecommend) {
        if (bookId > 0 && (isrecommend == 0 || isrecommend == 1)) {
            int res = bookService.changeBookisRecommend(bookId, isrecommend);
            if (res > 0) {
                return Result.success(200, "成功修改状态");
            } else {
                return Result.fail(201, "修改状态失败");
            }
        } else {
            return Result.fail(202, "错误请求");
        }
    }

    //    更新
    @RequestMapping("/updateBooks")
    public Result updateBook(@RequestBody Book book) {
        if (book != null) {
            int res = bookService.updateBooks(book);
            if (res > 0) {
                return Result.success(200, "修改成功");
            } else {
                return Result.fail(201, "更新失败");
            }
        } else {
            return Result.fail(202, "错误请求");
        }
    }

    //    模糊查询
    @RequestMapping("/searchByKey")
    public Result searchByKey(int page, int limit, String keyWord) {
        System.out.println("---->>>>" + page + "  " + limit + " " + keyWord);
        if (page > 0 && limit > 0) {
            PageHelper.startPage(page, limit);
            List<BookS> bookList = bookService.searchByKeyWord(keyWord);
            PageInfo<BookS> pageInfo = new PageInfo<>(bookList);
            return Result.success(200, pageInfo.getList(), "查询到以下信息");

        } else {
            return Result.success(202, "错误请求");
        }
    }


}
